package com.mzj.saas.mservice.property.service;

import com.mzj.saas.commons.ConvertUtils;
import com.mzj.saas.mservice.property.entity.EstateRepairsCost;
import com.mzj.saas.mservice.property.entity.EstateRepairsCostDetail;
import com.mzj.saas.mservice.property.repository.EstateRepairsCostDetailRepository;
import com.mzj.saas.mservice.property.vo.EstateRepairsCostDetailVO;
import com.mzj.saas.mservice.property.vo.EstateRepairsCostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hikari
 * @since 2021-05-20
 */
@Service
public class EstateRepairsCostDetailService {
    @Autowired
    private EstateRepairsCostDetailRepository estateRepairsCostDetailRepository;
    @Autowired
    private EstateRepairsCostService estateRepairsCostService;

    public EstateRepairsCostDetail save(EstateRepairsCostDetail detail) {
        return estateRepairsCostDetailRepository.save(detail);
    }

    public List<EstateRepairsCostDetail> findByRepairsId(Long repairsId) {
        return estateRepairsCostDetailRepository.findByRepairsId(repairsId);
    }

    /**
     * 封装报修数据明细金额
     * @param costDetail
     * @return
     */
    public EstateRepairsCostDetailVO convertToVO(EstateRepairsCostDetail costDetail) {
        EstateRepairsCostDetailVO vo = new EstateRepairsCostDetailVO();
        vo.setId(costDetail.getId());
        vo.setRepairsId(costDetail.getRepairsId());
        vo.setCostId(costDetail.getCostId());

        EstateRepairsCost cost = estateRepairsCostService.findById(vo.getCostId());
        EstateRepairsCostVO costVO = estateRepairsCostService.convertToVO(cost);
        vo.setEstateRepairsCostVO(costVO);

        vo.setMaterialsCost(ConvertUtils.fen2yuan(costDetail.getMaterialsCost()));
        vo.setServiceCost(ConvertUtils.fen2yuan(costDetail.getServiceCost()));
        vo.setTotalCost(ConvertUtils.fen2yuan(costDetail.getTotalCost()));

        return vo;
    }
}
